<?php

/**
 * Permissions storage class
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Permissions.php 38 2009-02-18 17:12:39Z monzee $
 */
class Admin_Array_Permissions
{
    protected $_permissions;

    public function __construct()
    {
        $this->_permissions = array(
            array(
                'resource_id' => 'admin/index',
                'role_id'     => 'admin', // Administrator
                'privilege'   => null,
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'default/index',
                'role_id'     => 'admin', // Administrator
                'privilege'   => null,
                'allowed'       => true,
            ),
            array(
                'resource_id' => 'default/index',
                'role_id'     => 'guest',
                'privilege'   => null,
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/posts',
                'role_id'     => 'author', // Author
                'privilege'   => null,
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/posts',
                'role_id'     => 'author', // Author
                'privilege'   => 'delete',
                'allowed'      => false,
            ),
            array(
                'resource_id' => 'blog/posts',
                'role_id'     => 'author', // Author
                'privilege'   => 'destroy',
                'allowed'     => false,
            ),
            array(
                'resource_id' => 'blog/posts',
                'role_id'     => 'editor', // Editor
                'privilege'   => 'delete',
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/posts',
                'role_id'     => 'editor', // Editor
                'privilege'   => 'destroy',
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/comments',
                'role_id'     => 'member', // Member
                'privilege'   => null,
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/comments',
                'role_id'     => 'member', // Member
                'privilege'   => 'delete',
                'allowed'     => false,
            ),
            array(
                'resource_id' => 'blog/comments',
                'role_id'     => 'member', // Member
                'privilege'   => 'destroy',
                'allowed'     => false,
            ),
            array(
                'resource_id' => 'blog/comments',
                'role_id'     => 'mod', // Moderator
                'privilege'   => 'delete',
                'allowed'     => true,
            ),
            array(
                'resource_id' => 'blog/comments',
                'role_id'     => 'mod', // Moderator
                'privilege'   => 'destroy',
                'allowed'     => true,
            ),
        );
    }

    /**
     * Returns the whole rows where a resource_id == $res['id']
     * 
     * @param array $res resource array
     * @return array containing roleId, resourceId, privilege and allow
     */
    public function getRulesByResource($res)
    {
        $permissions = $this->_permissions;
        $resId = $res['id'];

        $ret = array();
        foreach ($permissions as $rule) {
            if ($rule['resource_id'] == $resId) {
                $ret[] = $rule;
            }
        }

        return $ret;
    }

    /**
     * Return all rows
     *
     * @return array
     */
    public function getAll()
    {
        return $this->_permissions;
    }

}
